//
//  WLWebController.m
//  WangliBank
//
//  Created by 王启镰 on 16/6/21.
//  Copyright © 2016年 iSoftstone infomation Technology (Group) Co.,Ltd. All rights reserved.
//

#import "WYWebController.h"
#import "WYWebProgressLayer.h"
#import "UIView+Frame.h"

@interface WYWebController ()<WKNavigationDelegate>

@end

@implementation WYWebController
{
    WKWebView *_webView;
    
    WYWebProgressLayer *_progressLayer; ///< 网页加载进度条
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupUI];
}


- (void)setupUI {
    
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:OYImageNamed(@"back") style:UIBarButtonItemStyleDone target:self action:@selector(back)];
    backButton.imageInsets = UIEdgeInsetsMake(0, -10, 0, 0);
    
    self.navigationItem.leftBarButtonItems = @[backButton];
    
    
    _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - OYAppNavBarH)];
    _webView.navigationDelegate = self;
    //滑动返回看这里
    _webView.allowsBackForwardNavigationGestures = YES;
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.url]];
    [_webView loadRequest:request];
    
    _webView.backgroundColor = [UIColor whiteColor];
    
    _progressLayer = [WYWebProgressLayer new];
    _progressLayer.frame = CGRectMake(0, 42, SCREEN_WIDTH, 2);
    
    [self.navigationController.navigationBar.layer addSublayer:_progressLayer];
    [self.view addSubview:_webView];
}

#pragma mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation {
    [_progressLayer startLoad];
}

// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    
    if (webView.canGoBack) {
        UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:OYImageNamed(@"back") style:UIBarButtonItemStyleDone target:self action:@selector(back)];
        backButton.imageInsets = UIEdgeInsetsMake(0, -10, 0, 0);
        
        UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:OYImageNamed(@"close") style:UIBarButtonItemStyleDone target:self action:@selector(closeClick)];
        closeButton.imageInsets = UIEdgeInsetsMake(0, -20, 0, 0);
        
        self.navigationItem.leftBarButtonItems = @[backButton, closeButton];
    } else {
        UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:OYImageNamed(@"back") style:UIBarButtonItemStyleDone target:self action:@selector(back)];
        backButton.imageInsets = UIEdgeInsetsMake(0, -10, 0, 0);
        
        self.navigationItem.leftBarButtonItems = @[backButton];
    }
    
    [_progressLayer finishedLoad];
}

- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
    
    [_progressLayer finishedLoad];
}

// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(nonnull NSError *)error {
    
    [_progressLayer finishedLoad];
}

//在发送请求之前，决定是否跳转 如果不实现这个代理方法,默认会屏蔽掉打电话等url
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    
//    NSString *absoluteString = navigationAction.request.URL.absoluteString;
//    OYLog(@"%@", absoluteString)
    
    decisionHandler(WKNavigationActionPolicyAllow);
}

//在收到服务器的响应头，根据response相关信息，决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(nonnull void (^)(WKNavigationResponsePolicy))decisionHandler {

    OYLog(@"%@", navigationResponse.response.URL.absoluteString)

    decisionHandler(WKNavigationResponsePolicyAllow);
}

#pragma mark - 响应事件
- (void)back {
    if (_webView.canGoBack) {
        [_webView goBack];
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)closeClick {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - dealloc
- (void)dealloc {
    
    [_progressLayer closeTimer];
    [_progressLayer removeFromSuperlayer];
    _progressLayer = nil;
    OYLog(@"i am dealloc")
}

#pragma mark - getter
- (WKWebView *)webView {
    
    return _webView;
}

@end
